package com.tt.task;

import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

/**
 * @Title: ScheduledConfig
 * @Author zl
 * @Package com.oberyun.zhgc.datascreen.config
 * @Date 2025/7/29 9:23
 * @description: 定时任务配置类
 */

@Configuration
public class ScheduledConfig extends ThreadPoolTaskScheduler {
    /**
     * 自定义定时任务线程池
     */
    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        // 核心线程数
        scheduler.setPoolSize(5);
        // 线程名前缀
        scheduler.setThreadNamePrefix("custom-task-");
        // 等待终止时间
        scheduler.setAwaitTerminationSeconds(60);
        // 关闭时等待任务完成
        scheduler.setWaitForTasksToCompleteOnShutdown(true);
        // 取消后移除任务
        scheduler.setRemoveOnCancelPolicy(true);
        scheduler.setErrorHandler(t -> {
            // 自定义错误处理
            LoggerFactory.getLogger(ScheduledConfig.class)
                    .error("Custom task scheduler error", t);
        });
        // 必须初始化
        scheduler.initialize();
        return scheduler;
    }
    @Bean
    public MethodTaskScheduler methodTaskScheduler() {
        MethodTaskScheduler scheduler = new MethodTaskScheduler(new DefTaskExecutionListenerImpl());
        return scheduler;
    }


}
